# 加上 openpyxl 库之后，程序会一直卡在一个地方，不知道什么鬼情况，这段程序先封存起来——2022.5.16

import requests, time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import *
from openpyxl import load_workbook


def req(url, dire=False):
    headersvalue = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.50',
        }
    r = requests.get(url, timeout=10, headers=headersvalue)
    if dire:
        return r.status_code
    soup = BeautifulSoup(r.text, 'lxml')
    return soup

def test(item, results):
    try:
        code = req(item[1], dire=True)
        if code == 200:
            print(f'{item[0]} | 可正常访问')
        else:
            print(f'{item[0]} | {code} | {item[1]}')
            results.append(item)
    except:
        print(f'{item[0]} | 没有状态码的错误 | {item[1]}')
        results.append(item)

def confirm(results):
    browser = webdriver.Chrome()
    browser.maximize_window()
    for result in results:
        name, url = result
        browser.get('https://gualemang.com/')
        browser.find_element(By.XPATH, '//div[@class="input-group"]/input').send_keys(url)
        browser.find_element(By.XPATH, '//div[@class="input-group-append"]/button').click()
        try:
            browser.find_element(By.XPATH, '//div[@class="input-group"]/input').send_keys('——' + name)
        except NoSuchElementException:
            print('此网站的网址有问题')
            continue
        except:
            print('未知错误')
            continue
        print('现在检测的是：{}，网址：{}'.format(name, url))
        WebDriverWait(browser, 20).until_not(EC.text_to_be_present_in_element((By.ID, 'status-text'), '检测中'))
        print('检测情况：\n' + browser.find_element(By.ID, 'status-list').text.replace(' ', '   '))
        if browser.find_element(By.CLASS_NAME, 'code').text == '200':
            print('似乎国内能打开，正在 webdriver 中尝试打开。。。')
            browser_1 = webdriver.Chrome()
            try:
                browser_1.get(url)
            except TimeoutException:
                print('TimeoutException!!!')
            except WebDriverException:
                print('不是 webdriver 出现了问题，就是网站访问不了')
            except:
                print('未知错误')
            q = input('回车关闭：')
            if q == '':
                browser_1.quit()
                print('网站窗口已关闭')
        next = input('下一个：')

def write_excel(page_name, results):
    wb = load_workbook('d:/didiao.xlsx')
    ws = wb[page_name]
    date = time.strftime('%m-%d %H:%M:%S', time.localtime())
    existence = [cell.value for cell in ws['A']]
    for result in results:
        name, url = result
        if str(existence).find(name) == -1:                                        # 无记录则添加记录
            print('新增：', name, url)
            ws.append([name, url, date])
        else:
            row = existence.index(name) + 1
            content = [cell.value for cell in ws[row]]
            try:
                ws.cell(row, len(content[:content.index(None)])+1).value = date      # 网站已记录则更新日期
            except ValueError:
                ws.cell(row, len(content)+1).value = date
    wb.save('d:/didiao.xlsx')

def web_info(url):
    node_li = req(url).find_all('li', attrs={'class': 'app-item'})
    page_names = ['默认'] + [li.find('a').string for li in node_li]
    page_urls = ['https://shouku123.com/tiantian'] + [li.find('a').attrs['href'] for li in node_li]
    pages = tuple(zip(page_names, page_urls))
    t = [f'{i+1}. {page_names[i]}' for i in range(len(page_names)) ]
    print('{}\n{}\n{}'.format('-'*140, '   '.join(t), '-'*140))
    flag = True
    while flag:
        try:
            page_i= int(input('需要检查哪个？\n'))
            flag = False
        except:
            continue

    node_ul = req(page_urls[page_i-1]).find_all('ul', attrs={'class': 'list-group collapse in urls'})
    groups_dict = {}
    for ul in node_ul:
        node_span = ul.find_all('span', attrs={'title': '点击查看二维码'})
        items = []
        for span in node_span:
            ls = span.attrs['onclick'][18:-2].split("'")
            items.append((ls[1], ls[3]))
        groups_dict[ul.attrs['title']] = items
    flag = True
    while flag:
        try:
            choice = int(input('\n“{}” 主题下共有 {} 个分类，分别是：\n {} \n {} \n {} \n 0. 一次性测试全部          1. 单独测试分类\n做出选择：'.\
                       format(pages[n-1][0], len(groups_dict), '-'*140, '      '.join(groups_dict.keys()), '-'*140)))
            flag = False
        except:
            continue
    if choice:
        flag = True
        while flag:
            try:
                c = input('输入需要测试分类的名称：')
                if c in groups_dict.keys():
                    flag = False
            except:
                continue
        temp = {}
        temp[c], groups_dict = groups_dict[c], temp
    results, n = [], 1
    for key, value in groups_dict.items():
        print('\n现在开始检查分类 “{}” （{}/{}）：\n'.format(key, n, len(groups_dict)))
        n += 1
        for item in value:
            flag = True
            if flag:
                test(item, results)
    
    print('\n第一遍测试结束！结果汇总如下：\n'+'\n'.join([str(i) for i in results])) if results else print('\n测试结束，网站全部可以正常访问！')
    with open('d:/results.txt', 'w', encoding='utf-8') as f:
        f.write('\n'.join(['title: {}{}url: {}'.format(i[0], ' '*6, i[1]) for i in results]))
    
    write_excel(page_names[page_i], results)

    confirm(results)
        

web_info('https://shouku123.com/tiantian')
